#include <iostream>
#include <memory>
#include <thread>
#include "mysqlConn.h"
#include "ConnectionPool.h"
using namespace std;
// 数据库连接测试
int query()
{
    MysqlConn conn;
    conn.connect("tzk", "12345678", "mydatabase", "172.29.166.14");
    string sql = "insert into person values(7, 35, 'Female', 'Jerry')";
    bool flag = conn.update(sql);
    cout << "flag value " << flag << endl;
    sql = "select * from person";
    conn.query(sql);
    while (conn.next())
    {
        cout << conn.value(0) << " " << conn.value(1) << " " << conn.value(2) << " " << conn.value(3) << endl;
    }
    return 0;
}

// 1. 单线程，使用/不使用连接池
void op1(int begin, int end)
{
    for (int i = begin; i < end; i++)
    {
        MysqlConn conn;
        conn.connect("tzk", "12345678", "mydatabase", "172.29.166.14");
        char sql[1024] = {0};
        sprintf(sql, "insert into person values(%d, 35, 'Female', 'Jerry')", i);
        conn.update(sql);
    }
}

void op2(ConnectionPool *pool, int begin, int end)
{
    for (int i = begin; i < end; i++)
    {
        shared_ptr<MysqlConn> conn = pool->getConnection();
        char sql[1024] = {0};
        sprintf(sql, "insert into person values(%d, 35, 'Female', 'Jerry')", i);
        conn->update(sql);
    }
}

void test1()
{
#if 1
    steady_clock::time_point begin = steady_clock::now();
    op1(0, 5000);
    steady_clock::time_point end = steady_clock::now();
    auto length = end - begin;
    cout << "非连接线程池时耗时" << length.count() / 1000000 << "毫秒" << endl;
#else
    ConnectionPool *pool = ConnectionPool::getConnectPool();
    steady_clock::time_point begin = steady_clock::now();
    op2(pool, 0, 5000);
    steady_clock::time_point end = steady_clock::now();
    auto length = end - begin;
    cout << "连接线程池时耗时" << length.count() / 1000000 << "毫秒" << endl;
#endif
}
// 2. 多线程，使用/不使用连接池
void test2()
{
#if 1
    MysqlConn conn;
    conn.connect("tzk", "12345678", "mydatabase", "172.29.166.14");
    steady_clock::time_point begin = steady_clock::now();
    thread t1(op1, 0, 1000);
    thread t2(op1, 1000, 2000);
    thread t3(op1, 2000, 3000);
    thread t4(op1, 3000, 4000);
    thread t5(op1, 4000, 5000);
    t1.join();
    t2.join();
    t3.join();
    t4.join();
    t5.join();
    steady_clock::time_point end = steady_clock::now();
    auto length = end - begin;
    cout << "非连接线程池时耗时" << length.count() / 1000000 << "毫秒" << endl;
#else
    ConnectionPool *pool = ConnectionPool::getConnectPool();
    steady_clock::time_point begin = steady_clock::now();
    thread t1(op2, pool, 0, 1000);
    thread t2(op2, pool, 1000, 2000);
    thread t3(op2, pool, 2000, 3000);
    thread t4(op2, pool, 3000, 4000);
    thread t5(op2, pool, 4000, 5000);
    t1.join();
    t2.join();
    t3.join();
    t4.join();
    t5.join();
    steady_clock::time_point end = steady_clock::now();
    auto length = end - begin;
    cout << "连接线程池时耗时" << length.count() / 1000000 << "毫秒" << endl;
#endif
}
int main()
{
    // query();
    // test1();
    test2();
    return 0;
}